var control;

function Control(){
	this.debug = false;

	var UD = new UserData();
	var car = new DMACarousel();
	var MW = [];

	var week_cnt = 0;
	var that = this;

	this.getUD = function(){return UD;};
	this.getCarousel = function(){return car;};
	this.getMW = function(){return MW;};

	var fn_next = function(){
		if (window.step < window.quantSteps-1) {
			window.step++;
			$("#slider").animate({"margin-left": "-=" + (window.wrapperWidth + window.borderWidth)}, "slow");
		}
		else {
			$("#slider").animate({"margin-left": 0}, "slow");
			window.step = 0;
		}
	};

	var fn_previous = function(){
		if (window.step > 0) {
			window.step--;
			$("#slider").animate({"margin-left": "+=" + (window.wrapperWidth + window.borderWidth)}, "slow");
		}		
	};

	this.buttonClick = function(e){
		if(e.target.id=="next1" || e.target.id=="next2"){
			fn_next();
		}else if(e.target.id=="next3"){
			if(UD.getActors().length==2){
				fn_next();
				this.createWeeks();
			}
		}else if(e.target.id=="next4" || e.target.id=="next5" || e.target.id=="next6" || e.target.id =="next7" || e.target.id =="next8"){
			fn_next();
			this.nextWeek();
		}else if(e.target.id=="next9"){		
			fn_next();
			this.sendData();
		}
	}

	this.selectActor = function(actor){
		if( UD.selectActor(actor) ){
			car.selectActor(actor);
			if(UD.getActors().length==2){
				$("#next3").fadeIn();
			}
		}
	};

	this.reselectActors = function(){
		UD.reselectActors();
		car.reselectActors();
		$("#next3").fadeOut();
	}

	this.createWeeks = function(){
		UD.setGender( $("#select-genero").find(":selected")[0].value );
		MW = makeWeeks(UD.getGender(),UD.getActors());
		week_cnt = -1;
		this.nextWeek();
	}

	this.nextWeek = function(){
		if(week_cnt>=0){
			UD.addSelectedMovie(MW[week_cnt].getSelectedMovie());
		}

		week_cnt+=1;
		if(week_cnt<5){
			MW[week_cnt].render(document.getElementById("movie-week-" + (week_cnt+1) ));
			$(".btn.week.mostrar").fadeOut();
			$(".btn.week.next").fadeOut();			
		}else{
			this.showAnswers(document.getElementById("check-answers"));
		}
		
	}

	this.showAnswers = function(container){
		var movies = UD.getSelectedMovies();
		for(var i=0;i<movies.length;i++){
			var p = document.createElement("p");
			
			var em = document.createElement("em");
			em.appendChild(document.createTextNode("Semana " + (i+1) + ": "));
			p.appendChild(em);

			var str = movies[i].getName() + " (" + movies[i].getGender() + ") com: " + movies[i].getActors()[0].name + " e " + movies[i].getActors()[1].name; 
			p.appendChild(document.createTextNode(str));
			container.appendChild(p);
		}		
	}

	this.showMovies = function(){
		MW[week_cnt].allMovies();
	}

	var makeWeeks = function(gender,actors){
		var moviePool = new MoviePool();
		var actorPool = new ActorPool();
		var commentPool = new CommentsPool();
		
		var weeks = [];
		weeks.push(new MovieWeek());
		weeks.push(new MovieWeek());
		weeks.push(new MovieWeek());
		weeks.push(new MovieWeek());
		weeks.push(new MovieWeek());


		/*
		 *Semana 1: Todos os comentarios positivos
		 *Semana 2: Genero favorito com outros atores somente com comentario positivo
		 *Semana 3: Outro Genero Com atores favoritos com comentarios positivos somente
		 *Semana 4: Outro Genero Com Outros Atrores com comentarios positivos somente

		 *Semana 1: Ira me dizer o que prevalece: Genero ou Atores
		 *Semana 2,3,4: Ira me dizer o que prevalece entre Genero, Atores e Comentarios 
		*/

		var movie1CommentType = [ ["POSITIVE"],["NEGATIVE"],["POSITIVE"],["NEGATIVE"],["POSITIVE","NEGATIVE"] ];
		var movie2CommentType = [ ["POSITIVE"],["NEGATIVE"],["NEGATIVE"],["POSITIVE"],["POSITIVE","NEGATIVE"] ];
		var movie3CommentType = [ ["POSITIVE"],["POSITIVE"],["NEGATIVE"],["NEGATIVE"],["POSITIVE","NEGATIVE"] ];
		/*Outro Genero com Atores Favoritos*/
		for(var i=0;i<5;i++){
			var pivotMovie = moviePool.getRandomMovieByGender(gender,true);
			pivotMovie.setActors(actors);			
			for(var j=0;j<movie1CommentType[i].length;j++){pivotMovie.addComment( commentPool.getRandomCommentByType(movie1CommentType[i][j],pivotMovie.getComments()) )};
		
			weeks[i].addMovie(pivotMovie,1);
		}

		/*Genero favorito com outros atores*
		 *TEM QUE VIR PRIMEIRO QUE O OUTRO POIS TEM PRIORIDADE NA ESCOLHA DO GENERO. CASO CONTRARIO O GENERO NECESSARIO PODE FICAR
		 *ESCASSO DE FILMES.
		 */
		for(var i=0;i<5;i++){
			var movie = moviePool.getRandomMovieByGender(gender,false);
			var actor1 = actorPool.getRandomActor([actors[0].id-1,actors[1].id-1]);
			var actor2 = actorPool.getRandomActor([actors[0].id-1,actors[1].id-1,actor1.id-1]);			
			movie.setActors([actor1,actor2]);
			for(var j=0;j<movie2CommentType[i].length;j++){movie.addComment( commentPool.getRandomCommentByType(movie2CommentType[i][j],movie.getComments()) )};
			
			weeks[i].addMovie(movie,3);
		}				
		
		/*Outro Genero e outros atores*/
		for(var i=0;i<5;i++){
			var movie = moviePool.getRandomMovie();
			if(i==4)
				movie.setBlockbuster(true);

			var actor1 = actorPool.getRandomActor([actors[0].id-1,actors[1].id-1]);
			var actor2 = actorPool.getRandomActor([actors[0].id-1,actors[1].id-1,actor1.id-1]);			
			movie.setActors([actor1,actor2]);
			for(var j=0;j<movie3CommentType[i].length;j++) movie.addComment( commentPool.getRandomCommentByType(movie3CommentType[i][j],movie.getComments()) );
			
			weeks[i].addMovie(movie,2);
		}		
	

		for(var i=0;i<5;i++){
			weeks[i].randomize();
		}

		
		return weeks;
	}

	this.sendData = function(){
		var _data = {
			genero:UD.getGender(),
			ator_1:UD.getActors()[0].name,
			ator_2:UD.getActors()[1].name,
			semana_1:UD.getSelectedMovies()[0].getId(),
			semana_2:UD.getSelectedMovies()[1].getId(),
			semana_3:UD.getSelectedMovies()[2].getId(),
			semana_4:UD.getSelectedMovies()[3].getId(),
			semana_5:UD.getSelectedMovies()[4].getId()
		};

		jQuery.ajax({
			type:"POST",
			url:"/",
			data:_data,
			dataType:"json",
			success:that.success,
			error:that.error
		});
	}

	this.success = function(r){
		$("#carregando").fadeOut();
		$("#agradecimento").fadeIn();
	}

	this.error = function(r){
		alert(r);
	}

}

function UserData(){
	var actorSelected = [];
	var moviesSelected = [];
	var genderSelected;

	this.getActors = function(){return actorSelected;};
	this.getGender = function(){return genderSelected};
	this.setGender = function(gender){genderSelected=gender;};
	this.addSelectedMovie = function(movie){
		moviesSelected.push(movie);
	}
	this.getSelectedMovies = function(){return moviesSelected;};

	this.selectActor = function(actor){
		if(actorSelected.length>=2){
			alert("Eu sei que você adora " + actor.name + "! Mas somente dois atores podem ser escolhidos.");
		}else{
			var escolhido = false;
			for(var i=0;i<actorSelected.length;i++){
				if(actorSelected[i].id==actor.id){
					escolhido = true;
				}
			}

			if(!escolhido){
				document.getElementById("txt-atores").value += actor.name + " ";

				actorSelected.push(actor);	
				return true;			
			}
		}
		return false;
	}

	this.reselectActors = function(){
		actorSelected = [];
		document.getElementById("txt-atores").value = "";
	}
}
